package com.sf.iisp.bind.helper;

import com.sf.iisp.BindingException;
import com.sf.iisp.bind.domain.MessageHead;
import com.sf.iisp.bind.enums.State;
import com.sf.iisp.util.HttpRequest;
import com.sf.kafka.check.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qi on 16-7-16.
 */
public class SDTFHelper {

    private static final Logger LOGGER = LoggerFactory.getLogger(SDTFHelper.class);

    private MessageHead head;
    private String basicTransId;

    private String sdtfBindingUrl1;
    private String sdtfBindingUrl2;

    public SDTFHelper(MessageHead head, String... urls) {
        this.head = head;
        sdtfBindingUrl1 = urls[0];
        sdtfBindingUrl2 = urls[1];
    }

    public SDTFHelper(MessageHead head, String basicTransId, String... urls) {
        this.head = head;
//        this.basicTransId = StringUtils.replace(basicTransId, "-", "");
        this.basicTransId = basicTransId;
        sdtfBindingUrl1 = urls[0];
        sdtfBindingUrl2 = urls[1];
    }

    public String accessBindSvc(String number0, String number1, String virtualNo) throws BindingException {
        Map<String, String> params = new HashMap<>();
        params.put("Operation", "0");
        params.put("CorpId", "886293");
        params.put("User", "admin");
        params.put("Pwd", "cinteam123");
        params.put("ObjId", "2016");
        params.put("DataType", "data");
        params.put("DataKey", "Telparser");
        params.put("DataSpace", "05200200");
        URI uri = null;
        try {
            List<Message> msgs = new ArrayList<>();
            Message msg = new Message(head.getTransId(), head.getTransTimestamp(), head.getMsgType(), head.getSysCode(), number0, number1, virtualNo);

            msgs.add(msg);
            Map<String, List<Message>> noMap = new HashMap<>();
            noMap.put("list", msgs);

            String list = JsonUtil.writeValueAsString(noMap);

            params.put("List", list);


            uri = HttpRequest.getUrl(sdtfBindingUrl1, params);
            LOGGER.info("access bind url address = {}", uri);
            return HttpRequest.getHttpGetRequest(uri);
        } catch (Throwable e) {
            try {
                uri = HttpRequest.getUrl(sdtfBindingUrl2, params);
                LOGGER.info("reaccess bind url address = {}", uri);
                return HttpRequest.getHttpGetRequest(uri);
            } catch (Throwable e1) {
                LOGGER.error(e.getMessage(), e);
                throw new BindingException(State.NETWORK.toString(), e.getMessage());
            }

        }

    }

    public String accessUnbindSvc(String number0, String number1, String virtualNo) throws BindingException {
        Map<String, String> params = new HashMap<>();
        params.put("Operation", "1");
        params.put("CorpId", "886293");
        params.put("User", "admin");
        params.put("Pwd", "cinteam123");
        params.put("ObjId", "2016");
        params.put("DataType", "data");
        params.put("DataKey", "Telparser");
        params.put("WhereStr", "transId");
        params.put("DataSpace", "05200200");
        URI uri = null;

        try {
            List<Message> msgs = new ArrayList<>();

            Message msg = new Message(basicTransId, head.getTransTimestamp(), head.getMsgType(), head.getSysCode(), "1", number0, number1, virtualNo);
            msgs.add(msg);
            Map<String, List<Message>> noMap = new HashMap<>();
            noMap.put("list", msgs);

            String list = JsonUtil.writeValueAsString(noMap);

            params.put("List", list);

            uri = HttpRequest.getUrl(sdtfBindingUrl1, params);
            LOGGER.info("access unbind url address = {}", uri);
            return HttpRequest.getHttpGetRequest(uri);
        } catch (Throwable e) {
            try {
                uri = HttpRequest.getUrl(sdtfBindingUrl2, params);
                LOGGER.info("reaccess unbind url address = {}", uri);
                return HttpRequest.getHttpGetRequest(uri);
            } catch (Throwable e1) {
                LOGGER.error(e.getMessage(), e);

                throw new BindingException(State.NETWORK.toString(), e.getMessage());
            }

        }


    }

    public static MessageHead setHeadProperty(Message msg) {
        MessageHead head = new MessageHead();
        msg.setTransId(StringUtils.replace(head.getTransId(), "-", ""));
        msg.setTransTimestamp(head.getTransTimestamp());
        msg.setSysCode(head.getSysCode());
        msg.setMsgType(head.getMsgType());

        return head;
    }

    class Message extends MessageHead {
        private String status = "0";
        private String virtualPhone;
        private String telephone0;
        private String telephone1;

        public Message(String telephone0, String telephone1, String virtualPhone) {
            this.virtualPhone = virtualPhone;
            this.telephone0 = telephone0;
            this.telephone1 = telephone1;
            this.status = "0";
        }


        public Message(String status, String telephone0, String telephone1, String virtualPhone) {
            this.status = status;
            this.virtualPhone = virtualPhone;
            this.telephone0 = telephone0;
            this.telephone1 = telephone1;
        }

        public Message(String transId, String transTimestamp, String msgType, String sysCode, String telephone0, String telephone1, String virtualPhone) {
            super(transId, transTimestamp, msgType, sysCode);
            this.status = "0";
            this.virtualPhone = virtualPhone;
            this.telephone0 = telephone0;
            this.telephone1 = telephone1;
        }

        public Message(String transId, String transTimestamp, String msgType, String sysCode, String status, String telephone0, String telephone1, String virtualPhone) {
//            super(StringUtils.replace(transId, "-", ""), transTimestamp, msgType, sysCode);
            super(transId, transTimestamp, msgType, sysCode);
            this.status = status;
            this.virtualPhone = virtualPhone;
            this.telephone0 = telephone0;
            this.telephone1 = telephone1;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getVirtualPhone() {
            return virtualPhone;
        }

        public void setVirtualPhone(String virtualPhone) {
            this.virtualPhone = virtualPhone;
        }

        public String getTelephone0() {
            return telephone0;
        }

        public void setTelephone0(String telephone0) {
            this.telephone0 = telephone0;
        }

        public String getTelephone1() {
            return telephone1;
        }

        public void setTelephone1(String telephone1) {
            this.telephone1 = telephone1;
        }
    }


}
